package com.allwees.bs.module.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.ums.dao.CustomerDao;
import com.allwees.bs.module.ums.entity.Customer;
import com.allwees.bs.module.ums.req.CustomerUpdateInfluencerReq;
import com.allwees.bs.module.ums.req.CustomerUpdatePromCodeReq;
import com.allwees.bs.module.ums.req.CustomerUpdateShareCodeReq;
import com.allwees.bs.module.ums.req.query.CustomerQueryReq;
import com.allwees.bs.module.ums.service.CustomerService;
import com.allwees.bs.module.ums.util.PromoCodeUtil;
import com.allwees.core.common.constant.EStatus;
import me.zhengjie.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

@Service
public class CustomerServiceImpl extends BoServiceImpl<CustomerDao, Customer> implements CustomerService {

    @Override
    public Page<Customer> page(Page<Customer> page, CustomerQueryReq req) {
        if(req.getInfluencerType() != null ){
            req.setIsInfluencer(req.getInfluencerType().type());
        }
        return baseMapper.page(page, req);
    }

    @Override
    public void disable(String uuid) {
        Assert.hasLength(uuid, "uuid is not be null");
        updateStatusByUuids(EStatus.DISABLED, uuid);
    }
    @Override
    public void setInfluencerAndBelongName(String uuid, CustomerUpdateInfluencerReq req){
        Assert.hasLength(uuid, "uuid is not be null");
        //Assert.isTrue(!(StringUtils.isEmpty(req.getBelongName()) && req.getIsInfluencer() == null) ,"参数非法");
        Customer customer = findByUuid(uuid);
        Assert.isTrue(customer != null,"用户不存在");
        customer.setBelongName("");
        if (StringUtils.isNotEmpty(req.getBelongName())) {
            customer.setBelongName(req.getBelongName());
        }
        if (req.getIsInfluencer() != null) {
            customer.setIsInfluencer(req.getIsInfluencer().type());
        }
        baseMapper.updateByUuid(customer,uuid);
    }

    @Override
    public void enable(String uuid) {
        Assert.hasLength(uuid, "uuid is not be null");
        updateStatusByUuids(EStatus.NORMAL, uuid);
    }

    @Override
    public void refreshPromoCode(int count) {
        for (int i = 0; i <= count; i++) {
            IPage<Customer> page = new Page<>(i, 100);
            IPage<Customer> page1 = baseMapper.selectPage(page, new QueryWrapper<>());

            if (page1.getRecords().size() == 0) {
                break;
            }
            page1.getRecords().forEach(customer -> {
                customer.setPromoCode(PromoCodeUtil.idToCode(customer.getId()));
            });
            updateBatchById(page1.getRecords());
        }
    }

    @Override
    public void updatePromoCode(CustomerUpdatePromCodeReq req) {
        boolean hasPromoCode = baseMapper.findCountByPromoCode(req.getPromoCode()) > 0;
        Assert.isTrue(!hasPromoCode, "promo code exists");
        Customer update = new Customer();
        update.setPromoCode(req.getPromoCode());
        baseMapper.updateByUuid(update, req.getUuid());
    }


    @Override
    public void updateShareCode(CustomerUpdateShareCodeReq req) {

        Assert.isTrue(!StringUtils.isEmpty(req.getShareCode()), "promo code is empty");
        /**
         * 匹配4-10大写字母与数据组合格式校验
         */
        String reg = "^[A-Z0-9]{4,10}$";
        boolean r = req.getShareCode().matches(reg);

        Assert.isTrue(r, "promo code 4-10 capital letters and data combination");

        boolean hasShareCode = baseMapper.findCountByPromoCode(req.getShareCode()) > 0;
        Assert.isTrue(!hasShareCode, "promo code exists");
        Customer update = baseMapper.findByUuid(req.getUuid());
       // update.setShareCode(req.getShareCode());
        update.setPromoCode(req.getShareCode());
        baseMapper.updateByUuid(update, req.getUuid());
    }

    @Override
    public void saveOfIcs(Customer customer) {
        save(customer);

        if (StringUtils.isBlank(customer.getPromoCode())) {
            customer.setPromoCode(PromoCodeUtil.idToCode(customer.getId()));
            updateById(customer);
        }
    }
}
